//
//  RHTestCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/11/14.
//

#import "RHTestCell.h"
@interface RHTestCell()
@property (nonatomic ,strong) LineChartView *lineChartView;

@property (nonatomic ,strong) NSMutableArray *data;
@end
@implementation RHTestCell
#pragma mark ----- 创建曲线
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}
-(void)setupUI
{
    [self.contentView addSubview:self.lineChartView];
//    [self.lineChartView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_offset(15);
//        make.top.mas_offset(15);
//        make.right.mas_offset(-15);
//        make.height.offset(250);
//        make.bottom.mas_offset(-15);
//    }];
}

-(void)setXArr:(NSMutableArray*)xArr  yArr_1:(NSMutableArray*)yArr_1 yArr_2:(NSMutableArray*)yArr_2{
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    LineChartDataSet *set1 = [self createDataSetWithEntryArr:yArr_1 linecolor:@"3ECE80"];
    [dataSets addObject:set1];
    
    LineChartDataSet *set2 = [self createDataSetWithEntryArr:yArr_2 linecolor:@"E76262"];
    [dataSets addObject:set2];

    LineChartDataSet *set3 = [self createDataSetWithEntryArr:yArr_1 linecolor:@"3ECE80"];
    [dataSets addObject:set3];

    LineChartDataSet *set4 = [self createDataSetWithEntryArr:yArr_2 linecolor:@"E76262"];
    [dataSets addObject:set4];
    
    LineChartDataSet *set5 = [self createDataSetWithEntryArr:yArr_1 linecolor:@"3ECE80"];
    [dataSets addObject:set5];

    LineChartDataSet *set6 = [self createDataSetWithEntryArr:yArr_2 linecolor:@"E76262"];
    [dataSets addObject:set6];
     
   
    self.lineChartView.leftAxis.axisMaximum = 200;
//    if(self.lineChartView.data.dataSets.count != dataSets.count){
        NSLog(@"------11111绘制");
        LineChartData *lineData = [[LineChartData alloc] initWithDataSets:dataSets];
        self.lineChartView.data = lineData;
//    if(yArr_1.count>=300){
//        self.lineChartView.xAxis.labelCount = 100;
//
//    }else{
//        self.lineChartView.xAxis.labelCount = 10;
//    }
//       self.lineChartView.xAxis.labelCount = 10;
        //通知data去更新
//        [self.lineChartView.data notifyDataChanged];
//        //通知图表去更新
//        [self.lineChartView notifyDataSetChanged];
//    }else{
//        NSLog(@"------22222不绘制");
//    }
   
}



-(LineChartDataSet*)createDataSetWithEntryArr:(NSArray*)entryArr linecolor:(NSString*)colorStr;
{
    
    LineChartDataSet *set = [[LineChartDataSet alloc] initWithEntries:entryArr label:@"test"];
    [set setColor:kCOLOR_HEX(colorStr)];//折线颜色
    //set1.lineDashLengths = @[@4.0,@2.0];//设置虚线各段长度
//    set.mode = LineChartModeHorizontalBezier;//曲线平滑显示   贝塞尔曲线
    
    set.drawIconsEnabled = YES;//是否绘制图标
    set.formLineWidth = 1.1;//图例的线宽
    set.formSize = 8.0;//图例的字体大小
    set.lineWidth = 2.0;//折线的宽度
    

    set.drawCirclesEnabled = NO;//是否绘制拐点
    set.drawCircleHoleEnabled = YES;//是否画空心圆
    set.circleColors = @[kCOLOR_HEX(colorStr)];//外圆颜色
    set.circleHoleColor = UIColor.whiteColor; //内圆颜色
    set.circleRadius = 2; //外圆半径
    set.circleHoleRadius = 1; //内圆半径
    
    set.drawValuesEnabled = NO;//是否在拐点处显示数据
    set.valueColors = @[kCOLOR_HEX(colorStr)];//拐点上的文字颜色
    set.valueFont = [UIFont systemFontOfSize:8];//拐点上的文字大小
    
    //折线拐点样式
    //第二种填充样式:渐变填充
    set.drawFilledEnabled = YES;//是否填充颜色
    NSArray *gradientColors = @[(id)[UIColor colorWithHexString:colorStr alpha:0.15].CGColor,
                                (id)[UIColor colorWithHexString:colorStr alpha:0.0].CGColor];
    CGGradientRef gradientRef = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
    set.fillAlpha = 1.0f;//透明度
    set.fill = [[ChartLinearGradientFill alloc]initWithGradient:gradientRef angle:-90.0f];//赋值填充颜色对象
    CGGradientRelease(gradientRef);//释放gradientRef
    
    //点击选中拐点的交互样式
    set.highlightEnabled = YES;//选中拐点,是否开启高亮效果(显示十字线)
//    set.drawVerticalHighlightIndicatorEnabled = NO; //不显示纵向十字线
    set.drawHorizontalHighlightIndicatorEnabled = NO; //不显示横向十字线
    set.highlightColor = kRGB(23,23,23);//点击选中拐点的十字线的颜色
    set.highlightLineWidth = 1.1/[UIScreen mainScreen].scale;//十字线宽度
    set.highlightLineDashLengths = @[@5, @5];//十字线的虚线样式
    
    return set;
}

- (LineChartView *)lineChartView{
    if (!_lineChartView) {
        _lineChartView = [[LineChartView alloc] initWithFrame:CGRectMake(15, 15, (kUI_WINDOW_W-30), (kUI_WINDOW_W-30)*0.6)];
        _lineChartView.delegate = self;
        
        _lineChartView.chartDescription.enabled = NO;  //是否显示描述label
//        _lineChartView.extraRightOffset = 40;//右边缩进
        _lineChartView.extraBottomOffset = 10;//图表底部缩进,也就是与图例的距离
        _lineChartView.backgroundColor = kRGB(255, 255, 255);

        _lineChartView.noDataText = @"no data...";//没有数据时显示的文字
       
        _lineChartView.legend.enabled = YES;//是否显示说明图标
        _lineChartView.legend.form = ChartLegendFormSquare;//说明图例的样式  --指导价走势
        _lineChartView.legend.formSize = 3;//图例大小（头部）
        _lineChartView.legend.formToTextSpace = 5;//图例与文字的距离
        _lineChartView.legend.yOffset = 10;//图例与底部的距离
        _lineChartView.legend.xEntrySpace = 20;//图例与图例之间的距离
        _lineChartView.legend.neededHeight = 50;
        _lineChartView.legend.stackSpace = 10;
        _lineChartView.dragEnabled = YES; //拖动手势
        _lineChartView.dragDecelerationEnabled = YES;//拖拽后是否有惯性效果
        _lineChartView.dragDecelerationFrictionCoef = 0.9;//拖拽后惯性效果的摩擦系数(0~1)，数值越小，惯性越不明显
        _lineChartView.pinchZoomEnabled = YES; //捏合手势
//        _lineChartView.rightAxis.enabled = NO;  //隐藏右Y轴
        _lineChartView.doubleTapToZoomEnabled = NO;  //禁止双击缩放
        _lineChartView.drawGridBackgroundEnabled = NO; //绘制图形区域背景
        _lineChartView.drawBordersEnabled = NO; //绘制图形区域边框
        _lineChartView.scaleYEnabled = NO;//Y轴不允许缩放
        _lineChartView.highlightPerTapEnabled = YES;//高亮点击
        _lineChartView.highlightPerDragEnabled = NO;//高亮拖拽
//        [_lineChartView setVisibleXRangeMaximum:8];// 最大显示区域
//        [_lineChartView setVisibleXRangeMinimum:2];// 最小显示区域
//
        
//        _lineChartView.drawMarkers = YES;
//        //气泡标签
//        ChartMarkerView *marker = [[ChartMarkerView alloc]
//                                 init];
//        marker.offset = CGPointZero;
//        marker.chartView = _lineChartView;
//        marker.minimumSize = CGSizeMake(60.f, 20.f);
//        marker.image = [UIImage imageNamed:@""];
        
        
       
        //只滑动不缩放
    //    ChartViewPortHandler *scalX = self.LineChartView.viewPortHandler;
    //    [scalX setMinimumScaleX:1.01];
        
        
        //设置 左<--Y轴
        ChartYAxis *leftAxis =_lineChartView.leftAxis;
        leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
        leftAxis.axisLineWidth = 1;
        leftAxis.axisMaxLabels = 6;     //y轴展示多少个
        leftAxis.labelFont = [UIFont systemFontOfSize:12];
        leftAxis.labelTextColor = UIColor.lightGrayColor; //文本颜色
        leftAxis.axisLineColor = kRGB(217, 217, 217);    //左Y轴线条颜色
        leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//设置虚线样式的网格线
        leftAxis.gridColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//网格线颜色
        leftAxis.gridAntialiasEnabled = YES;//开启抗锯齿
        leftAxis.zeroLineColor = [UIColor redColor];    //左Y轴底线条颜色
        leftAxis.drawZeroLineEnabled = NO;//绘制0线
        
        leftAxis.axisMinimum = 0;//Y轴最小值
        leftAxis.axisMaximum = 100;   //Y轴最大值
        leftAxis.drawAxisLineEnabled = NO;
        
        //设置 右-->Y轴
        ChartYAxis *rightAxis =_lineChartView.rightAxis;
        rightAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
        rightAxis.axisLineWidth = 1;
        rightAxis.axisMaxLabels = 6;     //y轴展示多少个
        rightAxis.labelFont = [UIFont systemFontOfSize:12];
        rightAxis.labelTextColor = UIColor.lightGrayColor; //文本颜色
        rightAxis.axisLineColor = kRGB(217, 217, 217);    //左Y轴线条颜色
        rightAxis.gridLineDashLengths = @[@3.0f, @3.0f];//设置虚线样式的网格线
        rightAxis.gridColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//网格线颜色
        rightAxis.gridAntialiasEnabled = YES;//开启抗锯齿
        rightAxis.zeroLineColor = [UIColor redColor];    //左Y轴底线条颜色
        rightAxis.drawZeroLineEnabled = NO;//绘制0线
     
        rightAxis.axisMinimum = 0;//Y轴最小值
        rightAxis.axisMaximum = 100;   //Y轴最大值
        rightAxis.drawAxisLineEnabled = NO;
        
        // 设置X轴
        ChartXAxis *xAxis =_lineChartView.xAxis;
        xAxis.axisLineWidth = 1.0/[UIScreen mainScreen].scale;//设置X轴线宽
        xAxis.axisLineColor = UIColor.lightGrayColor;
        xAxis.labelPosition =XAxisLabelPositionBottom; //x轴文字的位置
        xAxis.labelFont = [UIFont systemFontOfSize:12];//文字字体
        xAxis.labelTextColor = UIColor.lightGrayColor;//文字颜色
        xAxis.labelCount = 6;
        xAxis.axisMaxLabels = 15;//x轴最多显示个数;
        xAxis.axisMinLabels = 5;//x轴最少显示个数;
        xAxis.granularity = 1;//x轴标题中间最小的间隔。
        xAxis.drawAxisLineEnabled = YES;//是否画x轴线
//        xAxis.granularityEnabled = YES;//设备重复不显示
//        xAxis.forceLabelsEnabled = NO; //数据点强制显示个数
//        xAxis.avoidFirstLastClippingEnabled = YES;//避免文字显示不全 这个属性很重要
        
        xAxis.drawGridLinesEnabled = NO;//是否画网格
        xAxis.gridColor = UIColor.redColor;//x轴对应网格线的颜色
        xAxis.gridLineWidth = 1;//x轴对应网格线的大小
        xAxis.gridLineDashLengths = @[@4,@2];  //虚线各段长度
    //    xAxis.labelRotationAngle = -30.0;//设置文字旋转角度
       

        //自定义数据显示格式
    //     NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    //    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    //    [formatter setPositiveFormat:@"#0.0"];
    //    formatter.positivePrefix = @"#";
    //    xAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc]initWithFormatter:formatter];
        
        
        //加载动画时长
//        [_lineChartView animateWithYAxisDuration:0.2 easingOption:ChartEasingOptionEaseInBack];
    }
    return _lineChartView;
}

@end
